//		ID : Xerxes
//      "I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,st,ed) for(int i = st ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define tp pair< int, pair<int,int> >
#define mmk( x, y , z ) mk( x, mk( y, z ) )

inline void wait( double seconds ){
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ){
        ;
    }
}
int R,C;
char grid[1005][1005];
int fs[1005][1005];
int js[1005][1005];
int m[4][2] = {{-1,0},{+1,0},{0,+1},{0,-1}};

inline void ini(){
	fo(i,0,R){
		fo(j,0,C){
			fs[i][j] = js[i][j] = (int)1023456789;
		}
	}
}


inline void Read(){
		scanf("%d %d\n",&R,&C);
		fo(i,0,R){
			fo(j,0,C){
				grid[i][j]=getchar();
			}
			getchar();
		}
		
}


inline void firebfs(){
	queue< pair<int,int> > q;
	fo(i,0,R){
		fo(j,0,C){
				if( grid[i][j]=='F' ){
					q.push( mk(i,j) );
					fs[i][j]=0;
				}
		}
	}
	
	while( q.empty()==false ){
		pair<int,int> t = q.front();
		q.pop();
		fo(i,0,4){
			int tr = t.f+m[i][0];
			int tc = t.s+m[i][1];
			if( tr < 0 || tc < 0 || tr >=R || tc >=C )continue;
			if( grid[tr][tc]=='#' )continue;
			
			if( fs[t.f][t.s]+1 < fs[tr][tc] ){
				q.push( mk(tr,tc) );
				fs[tr][tc] = fs[t.f][t.s]+1;
			}
		}
		
		
	}
}


inline void joebfs(){
	queue< pair<int,int> > q;
	fo(i,0,R){
		fo(j,0,C){
				if( grid[i][j]=='J' ){
					q.push( mk(i,j) );
					js[i][j]=0;
					goto L;
				}
		}
	}
L:
	while( q.empty()==false ){
		pair<int,int> t = q.front();
		q.pop();
		fo(i,0,4){
			int tr = t.f+m[i][0];
			int tc = t.s+m[i][1];
			if( tr < 0 || tc < 0 || tr >=R || tc >=C ){
				cout<<js[t.f][t.s]+1<<"\n";
				return ;
			}
			if( grid[tr][tc]=='#' )continue;
			if( js[t.f][t.s]+1 >= fs[tr][tc] )continue;
			if( js[t.f][t.s]+1 < js[tr][tc] ){
				q.push( mk(tr,tc) );
				js[tr][tc] = js[t.f][t.s]+1 ;
			}
		}
	}
	cout<<"IMPOSSIBLE\n";
}

inline void Print(){
	fo(i,0,R){
		fo(j,0,C){
			cout<<grid[i][j]<<" ";
		}
		cout<<"\n";
	}
}
inline void Proc(){
		ini();
		//Print();
		firebfs();
		joebfs();	
		
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in","rt",stdin);
    #endif
    int cases=1;
    for( CI(cases) ; cases ; --cases ){
        Read();
        Proc();
    }

return 0;
}


